package com.guess.lolguessing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guess.lolguessing.domain.po.Matches;
import com.guess.lolguessing.domain.po.Team;
import com.guess.lolguessing.domain.vo.MatchDetailsVO;
import com.guess.lolguessing.domain.vo.TeamVO;
import com.guess.lolguessing.mapper.MatchesMapper;
import com.guess.lolguessing.service.IMatchesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-09
 */
@Service
@RequiredArgsConstructor
public class MatchesServiceImpl extends ServiceImpl<MatchesMapper, Matches> implements IMatchesService {

    private final MatchesMapper matchesMapper;
    private final TeamServiceImpl teamService;
    private final PlayerServiceImpl playerService;
    private final GuessingServiceImpl guessingService;
    @Override
    public List<Matches> getMatches() {
        QueryWrapper<Matches> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("start_time"); // 按照比赛开始时间降序排列
        Page<Matches> page = new Page<>(1, 6); // 第 1 页，每页 6 条记录

        return matchesMapper.selectPage(page, queryWrapper).getRecords(); // 获取记录
    }

    @Override
    public MatchDetailsVO getMatchDetails(Long matchId) {
        MatchDetailsVO vo = new MatchDetailsVO();
        vo.setMatchId(matchId);
        Matches match = this.getById(matchId);
        Team teamA = teamService.getTeamByMatchId(match.getTeamAId());
        TeamVO teamAVO = new TeamVO();
        teamAVO.setName(teamA.getTeamName());
        teamAVO.setPlayers(playerService.findPlayers(Math.toIntExact(matchId), teamA.getTeamId()));
        teamAVO.setTeamId(teamA.getTeamId());

        Team teamB = teamService.getTeamByMatchId(match.getTeamBId());
        TeamVO teamBVO = new TeamVO();
        teamBVO.setName(teamB.getTeamName());
        teamBVO.setPlayers(playerService.findPlayers(Math.toIntExact(matchId), teamB.getTeamId()));
        teamBVO.setTeamId(teamBVO.getTeamId());

        Integer amountA = guessingService.getAmountTotalA(matchId, teamA.getTeamId());
        Integer amountB = guessingService.getAmountTotalB(matchId, teamB.getTeamId());
        if(!amountA.equals(0)  && !amountB.equals(0)) {
            teamAVO.setOdds( (double) (amountA + amountB) / amountA);
            teamBVO.setOdds((double) (amountA + amountB) / amountB);
        }
        if(!amountA.equals(0)  && amountB.equals(0)) {
            teamAVO.setOdds(1.0);
            teamBVO.setOdds(0.0);
        }
        if(amountA.equals(0)  && !amountB.equals(0)) {
            teamAVO.setOdds(0.0);
            teamBVO.setOdds(1.0);
        }
        if(amountA.equals(0)  && amountB.equals(0)) {
            teamAVO.setOdds(1.0);
            teamBVO.setOdds(1.0);
        }
        vo.setTeamA(teamAVO);
        vo.setTeamB(teamBVO);
        return vo;
    }
}
